package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Arrays;

import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.*;

/**
 * C -> S
 * 充电桩充电上传用户帐户查询报文
 * 充电桩充电上传用户帐户查询报文
 */
@Data
public class Cmd204 extends ProtocolDataBase {

    public static final int MIN_SIZE = 158;

    private int v1;
    private int v2;
    private String sn;                          // 充电桩编码
    private String chargingCardNum;             // 充电卡号
    private double balance;                     // 充电卡余额
    private int type;                           // 充电卡黑白名单标志
    private String pwd;                         // 用户充电卡密码
    private String random;                      // 卡扇区数据随机数
    private int serial;                         // M1卡序号
    private int authType;                       // 认证方式

    public Cmd204(){
        super.setCmd(204);
    }

    public static Cmd204 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd204 cmd = new Cmd204();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 32字节充电桩编码
        cmd.sn = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 32字节充电卡号
        cmd.chargingCardNum = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 4字节充点卡余额
        cmd.balance = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        // 1字节充电卡黑白名单标志
        cmd.type = getUint8(dataField[offset++]);
        // 32字节用户充点卡密码
        cmd.pwd = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 48卡扇区数据随机数
        cmd.random = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 48), true);
        offset += 48;
        // 4字节M1卡序号
        cmd.serial = bytesToIntLittle(dataField, offset);
        offset += 4;
        // 1字节认证方式
        cmd.authType =  getUint8(dataField[offset++]);
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd204#").append("\n");
        buffer.append("v1:").append(v1).append("\n");
        buffer.append("v2:").append(v2).append("\n");
        buffer.append("充电桩编码:").append(sn).append("\n");
        buffer.append("充电卡黑白名单标志:").append(type).append("\n");
        buffer.append("充电卡号:").append(chargingCardNum).append("\n");
                buffer.append("充电卡余额:").append(balance).append("\n");
                        buffer.append("用户充电卡密码:").append(pwd).append("\n"); 
                                buffer.append("卡扇区数据随机数:").append(random).append("\n");
                                        buffer.append("M1卡序号:").append(serial).append("\n"); 
                                                buffer.append("认证方式:").append(authType).append("\n");
        return buffer.toString();
    }
}
